package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.KfnWorkOrderStoreLvmhBo;
import com.ruoyi.system.domain.vo.KfnWorkOrderStoreLvmhVo;
import com.ruoyi.system.domain.KfnWorkOrderStoreLvmh;
import com.ruoyi.system.mapper.KfnWorkOrderStoreLvmhMapper;
import com.ruoyi.system.service.IKfnWorkOrderStoreLvmhService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * LVMH店铺Service业务层处理
 *
 * @author Panda
 * @date 2024-01-05
 */
@RequiredArgsConstructor
@Service
public class KfnWorkOrderStoreLvmhServiceImpl implements IKfnWorkOrderStoreLvmhService {

    private final KfnWorkOrderStoreLvmhMapper baseMapper;

    /**
     * 查询LVMH店铺
     */
    @Override
    public KfnWorkOrderStoreLvmhVo queryById(Long storeId){
        return baseMapper.selectVoById(storeId);
    }

    /**
     * 查询LVMH店铺列表
     */
    @Override
    public TableDataInfo<KfnWorkOrderStoreLvmhVo> queryPageList(KfnWorkOrderStoreLvmhBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnWorkOrderStoreLvmh> lqw = buildQueryWrapper(bo);
        Page<KfnWorkOrderStoreLvmhVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询LVMH店铺列表
     */
    @Override
    public List<KfnWorkOrderStoreLvmhVo> queryList(KfnWorkOrderStoreLvmhBo bo) {
        LambdaQueryWrapper<KfnWorkOrderStoreLvmh> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KfnWorkOrderStoreLvmh> buildQueryWrapper(KfnWorkOrderStoreLvmhBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnWorkOrderStoreLvmh> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getStoreNumber()), KfnWorkOrderStoreLvmh::getStoreNumber, bo.getStoreNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), KfnWorkOrderStoreLvmh::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), KfnWorkOrderStoreLvmh::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getDistricts()), KfnWorkOrderStoreLvmh::getDistricts, bo.getDistricts());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), KfnWorkOrderStoreLvmh::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getManageArea()), KfnWorkOrderStoreLvmh::getManageArea, bo.getManageArea());
        lqw.like(StringUtils.isNotBlank(bo.getStoreName()), KfnWorkOrderStoreLvmh::getStoreName, bo.getStoreName());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreType()), KfnWorkOrderStoreLvmh::getStoreType, bo.getStoreType());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreBrand()), KfnWorkOrderStoreLvmh::getStoreBrand, bo.getStoreBrand());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreStatus()), KfnWorkOrderStoreLvmh::getStoreStatus, bo.getStoreStatus());
        return lqw;
    }

    /**
     * 新增LVMH店铺
     */
    @Override
    public Boolean insertByBo(KfnWorkOrderStoreLvmhBo bo) {
        KfnWorkOrderStoreLvmh add = BeanUtil.toBean(bo, KfnWorkOrderStoreLvmh.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setStoreId(add.getStoreId());
        }
        return flag;
    }

    /**
     * 修改LVMH店铺
     */
    @Override
    public Boolean updateByBo(KfnWorkOrderStoreLvmhBo bo) {
        KfnWorkOrderStoreLvmh update = BeanUtil.toBean(bo, KfnWorkOrderStoreLvmh.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnWorkOrderStoreLvmh entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除LVMH店铺
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
